package org.lex.ftp.util.prefs;

import org.lex.ftp.FTP;
import org.lex.utils.text.Splitter;


public class FileActions {
	public static final FileAction DEFAULT_ACTION = FileAction.ASK;
	// operations
	public static final int DOWNLOAD = 0;
	public static final int UPLOAD = 1;
	public static final int FXP = 2;
	// -----------------------------
	// exists compare expected
	public static final int SMALLER = 0;
	public static final int SAME = 1;
	public static final int LARGER = 2;
	// ---------------------------------
	private static final char SEP = ',';
	private static final FileActions instance = new FileActions();

	public static FileActions getInstance() {
		return instance;
	}

	public static void loadDefaults(FileAction[][] rules) {
		for (int row = 0; row < 3; row++)
			for (int col = 0; col < 3; col++)
				rules[row][col] = DEFAULT_ACTION;
	}

	// ======================================================
	public FileAction getAction(int operation, int compare) {
		return rules[operation][compare];
	}

	public FileAction getAction(int operation, long exists, long expected) {
		int compare = SMALLER;
		if (exists < expected)
			compare = SMALLER;
		else if (exists == expected)
			compare = SAME;
		else if (exists > expected)
			compare = LARGER;
		return rules[operation][compare];
	}

	public FileAction[][] getActions() {
		FileAction[][] copy = new FileAction[3][3];
		for (int row = 0; row < 3; row++)
			for (int col = 0; col < 3; col++)
				copy[row][col] = rules[row][col];
		return copy;
	}

	public void setAction(FileAction action, int operation, int compare) {
		this.rules[operation][compare] = action;
		saveToResourceMap();
	}

	public void setActions(FileAction[][] actions) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				this.rules[i][j] = actions[i][j];
		saveToResourceMap();
	}

	public void restoreDefaults() {
		rules = new FileAction[3][3];
		loadDefaults(rules);
	}

	public void saveToResourceMap() {
		FTP.getContext().getResourceMap().putString(Keys.EXIST_RULES, this.encode());
	}

	/**
	 * 
	 * @param rules
	 *            һ��3x3�����飬�ֱ��Ӧ����DOWNLOAD/UPLOAD/FXP����ʱ��smaller, same,
	 *            largerʱ��Ӧ�Ĺ���
	 * @return
	 */
	public String encode() {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				buf.append(rules[i][j].name()).append(SEP);
		buf.setLength(buf.length() - 1);
		return buf.toString();
	}

	private FileAction[][] rules = null;

	private FileActions() {
		String str = FTP.getContext().getResourceMap().getString(Keys.EXIST_RULES);
		if (null == str || 0 == str.length())
			this.restoreDefaults();
		else {
			try {
				rules = decode(str);
			} catch (Exception e) {
				System.err.println("load file rules failed, using defaults");
				restoreDefaults();
			}
		}
	}

	private FileAction[][] decode(String str) {
		FileAction[][] rules = new FileAction[3][3];
		String[] tokens = Splitter.CommaSplitter.split(str);
		if (9 != tokens.length)
			throw new IllegalArgumentException();
		int i = 0;
		for (int row = 0; row < 3; row++)
			for (int col = 0; col < 3; col++)
				rules[row][col] = FileAction.valueOf(tokens[i++]);
		return rules;
	}
}
